package quartz.job.scheduled;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.scheduling.annotation.Schedules;

import java.util.concurrent.TimeUnit;

@Slf4j
@RequiredArgsConstructor
@Configuration(proxyBeanMethods = false)
public class Simple {
    @Async // 0 秒开始，每 3 秒执行一次
    @Scheduled(cron = "0/3 * * * * ?")
    void scheduledCron() {
        log.info("scheduledCron!");
    }

    @Async // 上一次执行完后，等固定间隔再执行
    @Scheduled(fixedDelay = 3000)
    void scheduledFixedDelay() {
        log.info("scheduledFixedDelay!");
    }

    @Async // 不管上一次是否执行完，等固定间隔执行
    @Scheduled(fixedRate = 3000)
    void scheduledFixedRate() {
        log.info("scheduledFixedRate!");
    }

    @Async // 延迟首次执行，单位默认毫秒，xxxString 支持 "${...}"
    @Scheduled(initialDelay = 3, fixedRateString = "3", zone = "Asia/Shanghai", timeUnit = TimeUnit.SECONDS)
    void scheduledInitialDelayFixedRateString() {
        log.info("scheduledInitialDelayFixedRateString!");
    }

    @Scheduled(initialDelay = 3000, fixedDelayString = "3000")
    void scheduledInitialDelayFixedDelayString() {
        log.info("scheduledInitialDelayFixedDelayString!");
    }

    @Schedules({@Scheduled(fixedDelay = 3000), @Scheduled(cron = "0/3 * * * * ?")})
    void schedules() {
        log.info("schedules!");
    }

    @Scheduled(fixedDelay = 3000)
    @Scheduled(cron = "0/3 * * * * ?")
    void scheduledScheduled() {
        log.info("scheduledScheduled!");
    }
}